//
//  RCRecommendFoodTypeView.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-9-21.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCRecommendFoodTypeView.h"
#import "NSArray+EX.h"
#import "CommonDefines.h"
#import "UIColor-Expanded.h"
#import "NSString+NSString_Ex_.h"

#define kBGimageFile        @"image_searchAllCity_cell_bg.png"
#define kLineImageFile      @"image_line_type3.png"

@interface RCRecommendFoodTypeView()

@property(nonatomic,retain)UITableView *typeTableView;
@property(nonatomic,retain)RCRecommendFoodTypeDataModel *dataModel;

@end

@implementation RCRecommendFoodTypeView

@synthesize typeTableView = _typeTableView;
@synthesize dataModel = _dataModel;

- (id)initWithFrame:(CGRect)frame andDataSource:(RCRecommendFoodTypeDataModel *)dataSource
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        if (!_typeTableView) {
            _typeTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) style:UITableViewStylePlain];
        }
        _typeTableView.backgroundColor = [UIColor whiteColor];
        _typeTableView.delegate = self;
        _typeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _typeTableView.dataSource = self;
        
        self.dataModel = dataSource;
        
        [self addSubview:_typeTableView];
    }
    return self;
}

-(void)dealloc{
    self.typeTableView.delegate = nil;
    self.typeTableView.dataSource = nil;
    self.typeTableView = nil;
    self.dataModel = nil;
    [super dealloc];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (nil == self.dataModel || nil == self.dataModel.typeItemList || [self.dataModel.typeItemList count] == 0) {
        return 0;
    }
    return [self.dataModel.typeItemList count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 39;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier = @"RCRecommendFoodTypeCell";
    RCPopViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (nil == cell) {
        cell = [[[RCPopViewTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]autorelease];
    }
    RCRecommendFoodTypeItem *item = [self.dataModel.typeItemList safeObjectAtIndex:[indexPath row]];
    if (nil == item) {
        return nil;
    }
    [cell setcontentViewWithString:item.typeName];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    RCRecommendFoodTypeItem *item = [self.dataModel.typeItemList safeObjectAtIndex:[indexPath row]];
    if (nil == item) {
        return;
    }
    if (_delegate && [_delegate respondsToSelector:@selector(selectFoodTypeItem:)]) {
        [_delegate selectFoodTypeItem:item];
    }
}

@end

@implementation RCPopViewTableViewCell
@synthesize contentLabel = _contentLabel;

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView setBackgroundColorWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kBGimageFile ofType:nil]]];
        _contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(14, 13, 290, 13)];
        _contentLabel.backgroundColor = [UIColor clearColor];
        _contentLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:13];
        _contentLabel.textColor = [UIColor colorWithHex:0xff505050];
        [_contentLabel setTextAlignment:NSTextAlignmentCenter];
        [self addSubview:_contentLabel];
        UIImageView *lineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 38, 320, 1)];
        [lineImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kLineImageFile ofType:nil]]];
        [self addSubview:lineImageView];
        [lineImageView release];
        self.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    return self;
}

-(void)dealloc{
    self.contentLabel = nil;
    [super dealloc];
}

-(void)setcontentViewWithString:(NSString *)str{
    if ([NSString stringIsNullOrEmpty:str]) {
        return;
    }
    _contentLabel.text = str;
}

@end
